package service;

import mapper.BrandMapper;
import pagination.PageBean;
import pojo.Brand;
import utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
/*
业务层
 */
public class BrandService {
    //品牌查询
    public List<Brand> selectAll(){
    //1.开启会话
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    SqlSession sqlSession = factory.openSession();
    //2.获取Mapper
    BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);
    //3.执行查询
    List<Brand> brandList = brandMapper.selectAll();
    //4.sqlSessin关闭
    sqlSession.close();
    //5.返回数据
    return  brandList;
    }
    //品牌新增
    public void add(Brand brand){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);
        //3.执行插入
        brandMapper.add(brand);
        //4.提交事务
        sqlSession.commit();
        //5.sqlSessin关闭
        sqlSession.close();
    }
    //单个查询
    public Brand selectById(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper =sqlSession.getMapper(BrandMapper.class);
        //3.执行查询
        Brand brand = brandMapper.selectById(id);
        //4.sqlSession关闭
        sqlSession.close();
        //5.返回数据
        return  brand;
    }

    //品牌修改
    public void update(Brand brand) {
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行修改
        brandMapper.update(brand);
        //4.提交事务
        sqlSession.commit();
        //5.sqlSession关闭
        sqlSession.close();
    }
    //删除
    public void delete(int id){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        //2. 获取SqlSession
        SqlSession sqlSession = factory.openSession();
        //3. 获取BrandMapper
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        //4. 调用方法
        mapper.deleteById(id);
        // 提交事务
        sqlSession.commit();
        // 释放资源
        sqlSession.close();
    }

    //分页品牌查询
    public PageBean<Brand> selectAllPage(int curPage,int pageSize){
        //1.开启会话
        SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
        SqlSession sqlSession = factory.openSession();
        //2.获取Mapper
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        //3.执行查询(做2个查询)
        // 【计算begin的索引】
        int beginIndex = (curPage -1 ) * pageSize;
        List<Brand> brandList = brandMapper.selectAllPage(beginIndex ,pageSize);
        int totalCount = brandMapper.selectCount();
        //3.3将数据封装为分页格式
        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setData(brandList);
        pageBean.setCurrentPage(curPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(totalCount);
        // 计算总页数
        int totalPage = (int) Math.ceil( totalCount*1.0/pageSize );
        pageBean.setTotalPage(totalPage);
        //4.sqlSession关闭
        sqlSession.close();
        //5.返回数据
        return pageBean;
    }
}
